package com.itcam.socket_.tcp_;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Author:Cammy.Wu
 * Date:2024-05-15
 * Description:加入完整的处理异常方式————客户端
 */

public class ClientDemo04 {
    public static void main(String[] args) {
        // 1.创建套接字：指定服务器的ip和端口号
        Socket socket = null;
        OutputStream outputStream = null;
        ObjectOutputStream objectOutputStream = null;
        InputStream inputStream = null;
        DataInputStream dataInputStream = null;
        try {
            socket = new Socket("192.168.1.12", 8888);

            // 录入账号密码
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入账号：");
            String name = sc.next();
            System.out.print("请输入密码：");
            String pwd = sc.next();

            // 将账号和密码封装为一个对象
            User user = new User(name, pwd);

            // 2.对程序员来说，向外发送数据 --》 利用输出流
            outputStream = socket.getOutputStream();
//        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            objectOutputStream = new ObjectOutputStream(outputStream);
            // 利用outputStream就可以向外发送数据了，但是没有直接发送String的方法
            // 所以在outputStream外套一个处理流dataOutputStream
//        dataOutputStream.writeUTF(name);
//        dataOutputStream.writeUTF(pwd);
            objectOutputStream.writeObject(user);


            // 接受服务器的绘画 --》 利用输入流
            inputStream = socket.getInputStream();
            dataInputStream = new DataInputStream(inputStream);
//        String str = dataInputStream.readUTF();
//        System.out.println("服务器对我说：" + str);
            boolean bool = dataInputStream.readBoolean();
            if (bool) {
                System.out.println("登录成功！！！");
            } else {
                System.out.println("登录失败！！！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3.关闭流
            try {
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
